#-------------------------------------------------------------------------------
# Name:        TextBox
# Purpose
#
# Author:      Theo
#
# Created:     24/01/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import pygame

# Four spaces good, two spaces bad! :D"

class TextBox():

  """ Textbox Class

  TextBox(screen surface, text font, displayed text, width, position(x,y)"""

  def __init__(self,screen,font,text,width,pos, color):
    self.screen = screen
    self.font = font
    self.text = text
    self.width = width - 3 #Add some whitespace to the end
    self.height = self.font.size("Aqyg|")[1] #tall and short letters
    self.text_width = 0
    self.total_text_width = 0
    self.area = pygame.Rect(pos,(self.width,self.height))
    self.focused = False
    self.curpos = (pos[0]+self.font.size(text)[0],pos[1])
    self.curtextpos = len(self.text)
    self.pos = pos
    self.count = 0
    self.switch = 1
    self.color = color
##    pygame.key.set_repeat(1,100) # lol, look how easy it is to fix the key holddown problem

  #TODO: Special characters and caps
  def draw(self,mousepos,leftclick,keypressed):

    """ Draw Textbox

    TextBox.draw(mouseposition(x,y), leftclick(True or False), keypressed(int))"""

    self.focused = 1
    leftclick = False

    #checking for shift keys
    mods = pygame.key.get_mods()
    if mods == 4097 or mods == 4098:
      shift = True
    else:
      shift = False
    mods = 0 #reset the variable

    self.text_width = 0
    self.total_text_width = 0
    c = 0
    shown_text = self.text

    #Adjust shown text
    while( self.font.size(self.text[c:])[0] > self.width):
      c = c + 1
      shown_text = self.text[c:]

    #Calculate text width
##    for i in range(c, self.curtextpos):
##      if self.text_width <= self.width:
##        self.text_width = self.text_width + self.font.size(self.text[i])[0]
    if self.text_width <= self.width:
      self.text_width = self.font.size(self.text[c:self.curtextpos])[0]
    dist = mousepos[0] - self.pos[0]
    buffer = 2 # add a buffer so the cursor isn't overlapping the letter
    self.curpos = (self.pos[0] + self.text_width + buffer,self.pos[1])

    #Place cursor according to mouse position
##    if leftclick == True and self.area.collidepoint(mousepos):
##      self.curtextpos = int(dist /self.font.size("a")[0])+1+c
##      if self.curtextpos > len(self.text):
##        self.curtextpos = len(self.text)
##      self.focused = 1
##    elif leftclick == True and self.area.collidepoint(mousepos) == False:
##      self.focused = 0
    if self.focused == 1:
      if keypressed is not 0: # just run a generic "Is a key actually being pressed" check here
        if keypressed == pygame.K_BACKSPACE and self.curtextpos > 0:
          if len(self.text) > 0:
            self.text = self.text[:self.curtextpos-1] + self.text[self.curtextpos:]
            self.curtextpos = self.curtextpos - 1
        #delete key works properly now
        if keypressed == pygame.K_DELETE and self.curtextpos > 0:
          if len(self.text) > 0:
            self.text = self.text[:self.curtextpos] + self.text[self.curtextpos+1:]
            self.curtextpos = self.curtextpos
        # Ascii codes from 32 through 126 are the only ones we need
        elif keypressed != -1 and keypressed <= 126 and keypressed >= 32 and keypressed != pygame.K_BACKSPACE and len(self.text) < 21:
          if shift == True:
            # Capital letters
            if keypressed >= 97 and keypressed <= 122:
              self.text = self.text[:self.curtextpos] + chr(keypressed - 32) + self.text[self.curtextpos:]
            # Misc other nonalpha chars
            elif keypressed >= 91 and keypressed <= 93:
              self.text = self.text[:self.curtextpos] + chr(keypressed + 32) + self.text[self.curtextpos:]
            elif keypressed == 49:
              self.text = self.text[:self.curtextpos] + chr(33) + self.text[self.curtextpos:]
            elif keypressed == 50:
              self.text = self.text[:self.curtextpos] + chr(64) + self.text[self.curtextpos:]
            elif keypressed >= 51 and keypressed <= 53:
              self.text = self.text[:self.curtextpos] + chr(keypressed -16) + self.text[self.curtextpos:]
            elif keypressed == 54:
              self.text = self.text[:self.curtextpos] + chr(94) + self.text[self.curtextpos:]
            elif keypressed == 55:
              self.text = self.text[:self.curtextpos] + chr(38) + self.text[self.curtextpos:]
            elif keypressed == 56:
              self.text = self.text[:self.curtextpos] + chr(42) + self.text[self.curtextpos:]
            elif keypressed == 57:
              self.text = self.text[:self.curtextpos] + chr(40) + self.text[self.curtextpos:]
            elif keypressed == 48:
              self.text = self.text[:self.curtextpos] + chr(41) + self.text[self.curtextpos:]
            elif keypressed == 59:
              self.text = self.text[:self.curtextpos] + chr(58) + self.text[self.curtextpos:]
            elif keypressed == 96:
              self.text = self.text[:self.curtextpos] + chr(126) + self.text[self.curtextpos:]
            elif keypressed == 44:
              self.text = self.text[:self.curtextpos] + chr(60) + self.text[self.curtextpos:]
            elif keypressed == 46:
              self.text = self.text[:self.curtextpos] + chr(62) + self.text[self.curtextpos:]
            elif keypressed == 47:
              self.text = self.text[:self.curtextpos] + chr(63) + self.text[self.curtextpos:]
            elif keypressed == 39:
              self.text = self.text[:self.curtextpos] + chr(34) + self.text[self.curtextpos:]
            elif keypressed == 45:
              self.text = self.text[:self.curtextpos] + chr(95) + self.text[self.curtextpos:]
            elif keypressed == 61:
              self.text = self.text[:self.curtextpos] + chr(43) + self.text[self.curtextpos:]
            elif keypressed == 32:
              self.text = self.text[:self.curtextpos] + chr(keypressed) + self.text[self.curtextpos:]
          else:
            self.text = self.text[:self.curtextpos] + chr(keypressed) + self.text[self.curtextpos:]
          self.curtextpos = self.curtextpos+1
        elif keypressed ==pygame.K_LEFT:
          self.curtextpos = self.curtextpos - 1
        elif keypressed == pygame.K_RIGHT:
          self.curtextpos = self.curtextpos + 1
        elif keypressed == pygame.K_UP:
          self.curtextpos = len(self.text)
        elif keypressed == pygame.K_DOWN:
          self.curtextpos = 0
    #Adjust cursor if negative or overflows
    if self.curtextpos < 0:
      self.curtextpos = 0
    elif self.curtextpos > len(self.text):
      self.curtextpos = len(self.text)

    # Background
    # Add 3 to widths, for whitespace
    pygame.draw.rect(self.screen,(191,191,191),pygame.Rect(self.pos[0],self.pos[1],self.width + 3,self.height + 1),0)
    pygame.draw.rect(self.screen,(self.color),pygame.Rect(self.pos[0] - 1,self.pos[1] - 1,self.width + 4,self.height + 2),1)
    pos = (self.pos[0] + 2, self.pos[1])
    # text
    self.screen.blit(self.font.render(shown_text,True,self.color),pos)
    # Cursor. Count is the number of frames to blink
##    if self.focused == 1:
##      if self.count == 10 and self.switch == 1:
##        self.count = 0
##        self.switch = 0
##        pygame.draw.line(self.screen,(0,0,0),(self.curpos[0], self.pos[1] + 1),(self.curpos[0],self.curpos[1] + self.height - 2))
##        pygame.draw.line(self.screen,(0,0,0),(self.curpos[0] - 1, self.pos[1] + 1),(self.curpos[0] + 1,self.curpos[1] + 1))
##        pygame.draw.line(self.screen,(0,0,0),(self.curpos[0] - 1, self.pos[1] + self.height - 2),(self.curpos[0] + 1,self.curpos[1] + self.height - 2))
##      elif self.count == 10 and self.switch == 0:
##        self.switch = 1
##        self.count = 0
##      elif self.switch == 1:
##        self.count += 1
##        pygame.draw.line(self.screen,(0,0,0),(self.curpos[0], self.pos[1] + 1),(self.curpos[0],self.curpos[1] + self.height - 2))
##        pygame.draw.line(self.screen,(0,0,0),(self.curpos[0] - 1, self.pos[1] + 1),(self.curpos[0] + 1,self.curpos[1] + 1))
##        pygame.draw.line(self.screen,(0,0,0),(self.curpos[0] - 1, self.pos[1] + self.height - 2),(self.curpos[0] + 1,self.curpos[1] + self.height - 2))
##      else:
##        self.count += 1


  def getText(self):
    """ Returns displayed text"""
    return self.text

  def setText(self, newText):
    """ Set displayed text."""
    self.text = newText
